<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\facade\Request;

class Upload extends Controller
{
    public function uploadthumb(){
        $file = request()->file('file');
        $info = $file->move('./upload');
        if($info){
            $image = \think\Image::open('./upload/'.$info->getSaveName());
            $width = $image->width();
            $height = $image->height();
            if($width<480 || $height<270){
                return json([
                    'success'=>true,
                    'code'=>1,
                    'msg'=>'图片分辨率不足480*270',
                ]);
            }
            $image->thumb(480, 270)->save('./upload/thumb/'.$info->getFilename());
            $imgData=[
                'res_name'=>$info->getFilename(),
                'res_path'=>Request::instance()->domain().'/upload/thumb/'.$info->getFilename(),
                'res_size'=>$info->getSize(),
                'res_type'=>$info->getExtension()
            ];
            $saveImg=Db::name('resources')->insert($imgData);
            if($saveImg){
                return json([
                    'success'=>true,
                    'code'=>0,
                    'msg'=>'上传成功',
                    'data'=>[
                        'src'=>$imgData['res_path']
                    ],
                    'file'=>$imgData['res_path'],
                    'file_path'=>$imgData['res_path']

                ]);
            }

        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }
    public function uploadvideo(){
        $file = request()->file('file');
        $info = $file->move('./upload');
        if($info){
            $imgData=[
                'res_name'=>$info->getFilename(),
                'res_path'=>Request::instance()->domain().'/upload/'.$info->getSaveName(),
                'res_size'=>$info->getSize(),
                'res_type'=>$info->getExtension()
            ];
            $saveImg=Db::name('resources')->insert($imgData);
            if($saveImg){
                return json([
                    'success'=>true,
                    'code'=>0,
                    'msg'=>'上传成功',
                    'data'=>[
                        'src'=>$imgData['res_path']
                    ],
                    'file'=>$imgData['res_path'],
                    'file_path'=>$imgData['res_path']

                ]);
            }

        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }
    public function uploadbook(){
        $file = request()->file('file');
        $info = $file->move('./upload');
        if($info){
            $image = \think\Image::open('./upload/'.$info->getSaveName());
            $width = $image->width();
            $height = $image->height();
            if($width<150 || $height<200){
                return json([
                    'success'=>true,
                    'code'=>1,
                    'msg'=>'图片分辨率不足480*270',
                ]);
            }
            $image->thumb(150, 200)->save('./upload/thumb/'.$info->getFilename());
            $imgData=[
                'res_name'=>$info->getFilename(),
                'res_path'=>Request::instance()->domain().'/upload/thumb/'.$info->getFilename(),
                'res_size'=>$info->getSize(),
                'res_type'=>$info->getExtension()
            ];
            $saveImg=Db::name('resources')->insert($imgData);
            if($saveImg){
                return json([
                    'success'=>true,
                    'code'=>0,
                    'msg'=>'上传成功',
                    'data'=>[
                        'src'=>$imgData['res_path']
                    ],
                    'file'=>$imgData['res_path'],
                    'file_path'=>$imgData['res_path']

                ]);
            }

        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }
    public function uploadduansp(){
        $file = request()->file('file');
        $info = $file->move('./upload');
        if($info){
            $image = \think\Image::open('./upload/'.$info->getSaveName());
            $width = $image->width();
            $height = $image->height();
            if($width<250 || $height<230){
                return json([
                    'success'=>true,
                    'code'=>1,
                    'msg'=>'图片分辨率不足250*230',
                ]);
            }
            $image->thumb(250, 230)->save('./upload/thumb/'.$info->getFilename());
            $imgData=[
                'res_name'=>$info->getFilename(),
                'res_path'=>Request::instance()->domain().'/upload/thumb/'.$info->getFilename(),
                'res_size'=>$info->getSize(),
                'res_type'=>$info->getExtension()
            ];
            $saveImg=Db::name('resources')->insert($imgData);
            if($saveImg){
                return json([
                    'success'=>true,
                    'code'=>0,
                    'msg'=>'上传成功',
                    'data'=>[
                        'src'=>$imgData['res_path']
                    ],
                    'file'=>$imgData['res_path'],
                    'file_path'=>$imgData['res_path']

                ]);
            }

        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }
}
